<?PHP
session_start();
error_reporting(E_ALL);
/* $Id: index.php 7 2009-03-24 20:25:52Z ronan $ */

//getting the user's access to see if they are allowed
include("includes/paxsql.php");
mysql_close();

include 'includes/database.php';
include 'includes/library.php';

function listMembers() {
	$query = "SELECT characters.charID
		FROM characters LEFT JOIN alts
		ON characters.charID = alts.charID
		WHERE alts.charID IS NOT NULL
		AND characters.inCorp = 1";
	$results = mysql_query($query) or die(mysql_error());
	$numAlt = mysql_num_rows($results);
	
	$query = "SELECT characters.charID
		FROM characters LEFT JOIN alts
		ON characters.charID = alts.charID
		WHERE alts.charID IS NULL
		AND characters.inCorp = 1
		AND characters.inactive = 1";
	$results = mysql_query($query);
	$numInactive = mysql_num_rows($results);
	
	$time30daysago = eveTime() - 60*60*24*30;
	$query = "SELECT characters.charID
		FROM characters LEFT JOIN alts ON characters.charID = alts.charID
		WHERE alts.charID IS NULL
		AND characters.inCorp = 1
		AND characters.joined > {$time30daysago}
		ORDER BY characters.name";
	$results = mysql_query($query);
	$numNew = mysql_num_rows($results);
	
	$time3daysago = eveTime() - 60*60*24*3;
	$query = "SELECT characters.charID
		FROM characters LEFT JOIN alts
		ON characters.charID = alts.charID
		WHERE alts.charID IS NULL
		AND characters.inCorp = 1
		AND characters.lastSeen > {$time3daysago}
		ORDER BY characters.name";
	$results = mysql_query($query);
	$numRecent = mysql_num_rows($results);

	$query = "SELECT distinct characters.charID, characters.lastSeen, characters.inactive, characters.joined
		FROM characters LEFT JOIN alts
		ON characters.charID = alts.charID
		WHERE alts.charID IS NULL
		AND characters.inCorp = 1
		ORDER BY characters.name";
	$results = mysql_query($query);
	$numMain = mysql_num_rows($results);
	
	echo "There are $numMain mains and $numAlt alts.<br>";
	echo "Of that, $numRecent mains have logged in within the past 72 hours.<br>";
	echo "Of that, $numNew mains are new.<br>";
	echo "Of that, $numInactive mains are inactive.<br><br>";
	
	echo "<table class=\"main\"><tr><td class=\"main\" width=\"70%\"><b>Name</b></td><td class=\"main\" width=\"30%\"><b>Last Seen</b></td></tr>";
	while ($char = mysql_fetch_array($results)) {
		/*** RENDER NAME ***/
		if ((eveTime() - $char['joined'] > 60*60*24*23) && (eveTime() - $char['joined'] < 60*60*24*32))
			echo "<tr style=\"background: #52c8f2;\">";
		elseif ($char['inactive'] == 1)
			echo "<tr style=\"background: #ffa9a9;\">";
		elseif (eveTime() - $char['lastSeen'] > 60*60*24*15)
			echo "<tr style=\"background: #fdff5e;\">";
		elseif (eveTime() - $char['joined'] < 60*60*24*30)
			echo "<tr style=\"background: #b9f0b9;\">";
		else
			echo "<tr>";
			
		echo "<td class=\"main\">";
		renderName($char['charID']);
		echo "</td>";
		
		/*** RENDER LAST SEEN ***/
		echo "<td class=\"main\">";
		$timePassed = eveTime() - $char['lastSeen'];
		
		if ($timePassed < 3600) echo "-" . round($timePassed/60) . " mins";
		else if ($timePassed < 172800) echo "-" . round($timePassed/3600, 2) . " hrs";
		else echo "-" . round($timePassed/86400, 2) . " days";	
		echo "</td></tr>";
	}
	echo "</table>";
}

if($access > 1)
{
echo("
<html>
<head>
<link rel='stylesheet' type='text/css' href='main.css'>
<title>snowflake</title>");

include 'includes/javascript.php';
echo("
</head>

<body>

<div id='isAltWin' style='display: none;'></div>
<div id='flagWin' style='display: none;'></div>

<div id='wrapper'>

	<div id='header'><a href='/'>snowflake</a></div>

	<div id='navi'>");
	include('includes/navi.php');
	echo("
	<table width:150px;style='text-align:center;'>
	<tr><td style = 'text-align:center;'><br />Reference</td></tr>
	<tr><td style='background-color:#b9f0b9;' width = '20%'>< 30d in corp</td></tr>
	<tr><td style='background-color:#ffa9a9;' width = '20%'>> 30d with no login</td></tr>
	<tr><td style='background-color:#52c8f2;' width = '20%'>25-35d in corp</td></tr>
	<tr><td style='background-color:#fdff5e;' width = '20%'>15-30d with no login</td></tr>
	</table><br /><br />");
	$result = mysql_query("SELECT updateTime FROM time WHERE updateTime > 0");	
	$row = mysql_fetch_array($result);
	echo("Snowflake last updated: ".gmdate('M d y H:i:s',$row[0]));
	echo("
	</div>

	<div id='main'>
		<h1>members</h1>
		<hr>");
listMembers();
	echo("</div>
	
</div>

</body>

</html>");
}
//they do not belong here, they get the boot
else
{
	header("Location: http://".$_SERVER['HTTP_HOST']."/index2.php");
}
mysql_close();
?>
